/**
 * 
 */
package com.hahaduo.bee.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hahaduo.bee.dao.AccountTypeDao;
import com.hahaduo.bee.dao.jpa.entity.AccountType;
import com.hahaduo.bee.dto.AccountTypeDto;
import com.hahaduo.bee.service.AccountTypeService;
import com.hahaduo.bee.web.form.AccountTypeForm;

/**
 * @author noodles.wumu@gmail.com
 * @since 2013/06/13 16:12:26
 * @version 0.1
 */
@Service
public class AccountTypeServiceImpl implements AccountTypeService {

	/**
	 * 
	 */
	@Resource
	private AccountTypeDao dao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hahaduo.bee.service.AccountTypeService#add(com.hahaduo.bee.web.form
	 * .AccountTypeForm)
	 */
	@Override
	public void add(AccountTypeForm af) {
		dao.add(af.getAccountTypeDto());
	}

	/* (non-Javadoc)
	 * @see com.hahaduo.bee.service.AccountTypeService#getAll(boolean)
	 */
	@Override
	public List<AccountTypeDto> getAll(boolean isCascade) {
		List<AccountType> list = dao.getAll();

		List<AccountTypeDto> dtoList = new ArrayList<AccountTypeDto>();
		AccountTypeDto d = null;

		List<AccountTypeDto> dtoListParent = new ArrayList<AccountTypeDto>();

		for (AccountType accountType : list) {
			d = new AccountTypeDto();
			d.setId(String.valueOf(accountType.getId()));
			d.setLabel1(accountType.getLabel1());
			d.setLabel2(accountType.getLabel2());
			d.setName(accountType.getName());
			if (accountType.getParentId() != null) {
				d.setParentId(String.valueOf(accountType.getParentId()));
			} else {
				dtoListParent.add(d);
			}
			dtoList.add(d);
		}

		if (isCascade) {
			for (AccountTypeDto dto : dtoList) {
				for (AccountTypeDto parent : dtoListParent) {
					if (parent.getId().equals(dto.getParentId())) {
						parent.getChilden().add(dto);
					}
				}
			}
			return dtoListParent;
		} else {
			List<AccountTypeDto> dList = new ArrayList<AccountTypeDto>();
			
			for (AccountTypeDto accountTypeDto : dtoList) {
				if (accountTypeDto.getParentId() == null) {
					dList.add(accountTypeDto);
				}
			}
			return dList;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hahaduo.bee.service.AccountTypeService#get(java.lang.String)
	 */
	@Override
	public AccountTypeDto get(String id) {
		AccountType accountType = dao.get(Long.parseLong(id));

		AccountTypeDto d = new AccountTypeDto();
		d.setId(String.valueOf(accountType.getId()));
		d.setLabel1(accountType.getLabel1());
		d.setLabel2(accountType.getLabel2());
		d.setName(accountType.getName());
		if (accountType.getParentId() != null) {
			d.setParentId(String.valueOf(accountType.getParentId()));
		}
		return d;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hahaduo.bee.service.AccountTypeService#delete(java.lang.String)
	 */
	@Override
	public void delete(String id) {
		dao.delete(Long.parseLong(id));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hahaduo.bee.service.AccountTypeService#update(com.hahaduo.bee.web
	 * .form.AccountTypeForm)
	 */
	@Override
	public void update(AccountTypeForm af) {
		dao.update(af.getAccountTypeDto());
	}

}
